package cn.xspace.module.other.adapter;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;


import java.util.List;
import java.util.Map;

import cn.xspace.module.other.R;
import cn.xspace.module.other.activity.MyFavoriteManagerActivity;
import cn.xspace.module.other.bean.FavoriteFolderBean;

public class FavoriteManagerAdapter extends BaseExpandableListAdapter {
    Context mContext;
    List<FavoriteFolderBean> parentlist;
    Map<String, List<FavoriteFolderBean>> childrenlist;
    MyFavoriteManagerActivity act;

    public FavoriteManagerAdapter(MyFavoriteManagerActivity act,
                                  List<FavoriteFolderBean> parentlist,
                                  Map<String, List<FavoriteFolderBean>> childrenlist) {
        super();
        this.mContext = act;
        this.parentlist = parentlist;
        this.childrenlist = childrenlist;
        this.act = act;
    }

    private IRightClickListener iRightClickListener;

    public void setiRightClickListener(IRightClickListener iRightClickListener) {
        this.iRightClickListener = iRightClickListener;
    }

    public interface IRightClickListener {
        void onRightClick(View v, int mode, FavoriteFolderBean fwb, boolean isChild);

        void onAddClick(FavoriteFolderBean fwb);
    }

    @Override
    public Object getChild(int arg0, int arg1) {
        return childrenlist.get(getGroupIdStr(arg0)).get(arg1);
    }

    @Override
    public long getChildId(int arg0, int arg1) {
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
                             View convertView, ViewGroup parent) {
        final FavoriteFolderBean fwb = (FavoriteFolderBean) getChild(groupPosition, childPosition);
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.favorite_manager_item, null);
        }
        convertView.setBackgroundResource(R.drawable.bg_fav_child_item);
        final int mode = act.getMode();
        ImageView ivLeft = (ImageView) convertView.findViewById(R.id.favorite_weblist_pop_item_img);
        TextView tvTitle = (TextView) convertView.findViewById(R.id.favorite_weblist_pop_item_tv);
        ImageView ivRight = (ImageView) convertView.findViewById(R.id.favorite_weblist_pop_item_right);
        View tab = convertView.findViewById(R.id.view_tab);
        tab.setVisibility(View.VISIBLE); //子控件,缩进
        ivLeft.setVisibility(View.GONE); //左图标不显示
        if (fwb.isIslastone()) { //如果是新建的条目,最后一个条目
            ivRight.setVisibility(View.GONE);
            if (mode == 0) {//收藏文章,收藏夹管理,移动收藏内容
                tvTitle.setText(R.string.create_new_folder_and_save);
            } else if (mode == 1) {
                tvTitle.setText(R.string.create_new_folder);
            } else if (mode == 2) {
                tvTitle.setText(R.string.create_new_folder_and_move);
            }
//            tvTitle.setTextColor(ResourceUtils.getColor(R.color.theme_color));
        } else { //普通子条目
            tvTitle.setText(fwb.getFolderName());
//            tvTitle.setTextColor(0xff333333);
            ivRight.setVisibility(View.VISIBLE);
            if (mode == 0) { //收藏文章
                ivRight.setImageResource(R.drawable.favorite_item_saved);
            } else if (mode == 1) { //收藏夹管理
                ivRight.setImageResource(R.drawable.favorite_item_more);
            } else if (mode == 2) { //移动文件夹
                ivRight.setImageResource(R.drawable.favorite_item_right_move);
            }
            ivRight.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    iRightClickListener.onRightClick(v, mode, fwb, true);
                }
            });
        }
        // 目前就新建收藏夹时用到
        convertView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                if (fwb.isIslastone()) {
                    iRightClickListener.onAddClick(fwb);
                }
            }
        });

        // 删除
//        tv_delete.setOnClickListener(new OnClickListener() {
//
//            @Override
//            public void onClick(View arg0) {
//                showDeleteDialog(fwb.getFolderId() + "");
//            }
//        });

        // 更多
//        tv_more.setOnClickListener(new OnClickListener() {
//
//            @Override
//            public void onClick(View arg0) {
//                showPopUp(arg0, false, fwb.getFolderId() + "");
//            }
//        });
//        tv_collect.setOnClickListener(new OnClickListener() {
//
//            @Override
//            public void onClick(View arg0) {
//                act.favoriteWebPage(fwb.getFolderId() + "");
//
//            }
//        });

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    @Override
    public int getChildrenCount(int i) {

        String key = getGroupIdStr(i);
        List<FavoriteFolderBean> tl = childrenlist.get(key);
        if (tl != null) {
            return tl.size();
        }
        return 0;
    }

    @Override
    public Object getGroup(int i) {
        return parentlist.get(i);
    }

    @Override
    public int getGroupCount() {
        return parentlist.size();
    }

    @Override
    public long getGroupId(int i) {
        return i;
    }

    public String getGroupIdStr(int arg0) {
        FavoriteFolderBean fwb = (FavoriteFolderBean) getGroup(arg0);
        return fwb.getFolderId();
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

        final FavoriteFolderBean fwb = (FavoriteFolderBean) getGroup(groupPosition);
        final int pos = groupPosition;
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.favorite_manager_item, null);
        }
        final int mode = act.getMode();
        ImageView ivLeft = (ImageView) convertView.findViewById(R.id.favorite_weblist_pop_item_img);
        TextView tvTitle = (TextView) convertView.findViewById(R.id.favorite_weblist_pop_item_tv);
        ImageView ivRight = (ImageView) convertView.findViewById(R.id.favorite_weblist_pop_item_right);

        if (fwb.isIslastone()) { //如果是新建的条目,最后一个条目
            ivRight.setVisibility(View.GONE);
            if (mode == 0) {//收藏文章,收藏夹管理,移动收藏内容
                tvTitle.setText(R.string.create_new_folder_and_save);
            } else if (mode == 1) {
                tvTitle.setText(R.string.create_new_folder);
            } else if (mode == 2) {
                tvTitle.setText(R.string.create_new_folder_and_move);
            }
//            tvTitle.setTextColor(ResourceUtils.getColor(R.color.theme_color));
        } else { //普通父条目
            tvTitle.setText(fwb.getFolderName());
//            tvTitle.setTextColor(0xff333333);
            ivRight.setVisibility(View.VISIBLE);
            if (mode == 0) { //收藏文章
                ivRight.setImageResource(R.drawable.favorite_item_saved);
            } else if (mode == 1) { //收藏夹管理
                ivRight.setImageResource(R.drawable.favorite_item_more);
            } else if (mode == 2) { //移动文件夹
                ivRight.setImageResource(R.drawable.favorite_item_right_move);
            }
        }

        if (getChildrenCount(groupPosition) == 0) {
            ivLeft.setVisibility(View.INVISIBLE); //没有孩子时不显示箭头
        } else { //有孩子时显示
            ivLeft.setVisibility(View.VISIBLE);
            if (!isExpanded) {
                ivLeft.setImageResource(R.drawable.green_triangle_right);
            } else {
                ivLeft.setImageResource(R.drawable.green_triangle_down);
            }
        }
        ivRight.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                iRightClickListener.onRightClick(v, mode, fwb, false);
            }
        });
        tvTitle.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (getChildrenCount(pos) == 0 && fwb.isIslastone()) {
                    iRightClickListener.onAddClick(fwb);
                }
            }
        });
//        tv_add.setOnClickListener(new OnClickListener() {
//
//            @Override
//            public void onClick(View arg0) {
//                showCreateFolderDialog(fwb.getFolderId());
//            }
//        });
//        // 更多
//        tv_more.setOnClickListener(new OnClickListener() {
//
//            @Override
//            public void onClick(View arg0) {
//                showPopUp(arg0, true, fwb.getFolderId() + "");
//            }
//        });
        return convertView;
    }

//    private void showCreateFolderDialog(final String parentid) {
//        final EditText editText = new EditText(mContext);
//        new AlertDialog.Builder(mContext).setTitle("请输入新建文件夹名")
//                .setIcon(android.R.drawable.ic_dialog_info).setView(editText)
//                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
//
//                    public void onClick(DialogInterface dialog, int which) {
//                        act.createFolder(editText.getText().toString(),
//                                parentid);
//                    }
//                }).setNegativeButton("取消", null).show();
//
//    }
//
//    private void showRenameFolderDialog1(final String folderid) {
//        final EditText editText = new EditText(mContext);
//        new AlertDialog.Builder(mContext).setTitle("请输入新文件夹名")
//                .setIcon(android.R.drawable.ic_dialog_info).setView(editText)
//                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
//
//                    public void onClick(DialogInterface dialog, int which) {
//                        String name = editText.getText().toString();
//                        if (!TextUtils.isEmpty(name)) {
//                            act.modifyFolderName(folderid, name);
//                        } else {
//                            Toast.makeText(mContext, "名称不可为空", Toast.LENGTH_SHORT).show();
//                        }
//                    }
//                }).setNegativeButton("取消", null).show();
//    }
//
//    private void showDeleteDialog(final String folder_id) {
//        new AlertDialog.Builder(mContext).setTitle("提示")
//                .setMessage("确定要删除此文件夹吗?")
//
//                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
//
//                    public void onClick(DialogInterface dialog, int which) {
//                        act.deleteFolder(folder_id);
//                    }
//                }).setNegativeButton("取消", null).show();
//
//    }
//
//    PopupWindow popup = null;
//
//    private void showPopUp(View v, boolean isdelete, final String fold_id) {
//        WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
//        int width = wm.getDefaultDisplay().getWidth();
//        View view = View.inflate(mContext, RProxy.manage_favorite_folder_popup, null);
//        TextView tv_delete = (TextView) view
//                .findViewById(RProxy.manager_favorite_delete_item);
//        TextView tv_rename = (TextView) view
//                .findViewById(RProxy.manager_favorite_folder_rename);
//        popup = new PopupWindow(mContext);
//        popup.setContentView(view);
//        popup.setWidth(width - 30);
//        popup.setHeight(100);
//        popup.setOutsideTouchable(true);
//        if (!isdelete) {
//            tv_delete.setVisibility(View.GONE);
//        }
//        tv_delete.setOnClickListener(new OnClickListener() {
//
//            @Override
//            public void onClick(View arg0) {
//                popup.dismiss();
//                showDeleteDialog(fold_id);
//
//            }
//        });
//        tv_rename.setOnClickListener(new OnClickListener() {
//
//            @Override
//            public void onClick(View arg0) {
//                popup.dismiss();
//                showRenameFolderDialog1(fold_id);
//
//            }
//        });
//        popup.showAsDropDown(v);
//
//    }
}
